package com.longxi.tianjintianbao.greedao.controller;


import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.greedao.dao.UserDao;
import com.longxi.tianjintianbao.greedao.entity.User;

import java.util.List;

/**
 * Created by 70339 on 2018/5/16.
 * 代码无八阿哥
 * 阿弥陀佛
 */
public class DataUserController {

    private static UserDao mUserDao = MyApplication.getInstance().getDaoSession().getUserDao();

    /**
     * 增加用户
     */
    public static long insertUser(User user)
    {
        return mUserDao.insertOrReplace(user);
    }

    /**
     * 查询用户
     */
    public static User findUserByIptypeAndUserid(User user)
    {
        User oldUser = mUserDao.queryBuilder()
                .where(UserDao.Properties.Iptype.eq(user.getIptype()))
                .where(UserDao.Properties.Userid.eq(user.getUserid()))
                .unique();
        return oldUser;
    }

    public static User findById(long id){
        User user = mUserDao.queryBuilder()
                .where(UserDao.Properties.Id.eq(id))
                .unique();
        return user;
    }

    public static User findByIslogin(String isLogin){
        User user = mUserDao.queryBuilder()
                .where(UserDao.Properties.IsLogin.eq(isLogin))
                .unique();
        return user;
    }

    public static void deleteByUser(User user){
        mUserDao.delete(user);
    }

    /**
     * 查询所有用户
     */
    public static List<User> selectUserList()
    {
        List<User> userList = mUserDao.queryBuilder()
                .orderDesc(UserDao.Properties.UpdateTime)
                .list();
        return userList;
    }

    /**
     * 更新用户资料
     */
    public static void updateUser(User user)
    {
        mUserDao.update(user);
    }

    /**
     * 将用户登录状态更新为0
     */
    public static void updateAllByIsLogin(){
        String sql ="update USER set IS_LOGIN = '0'"
                + " where IS_LOGIN = '1'" ;
        MyApplication.db.execSQL(sql);
    }
}
